// MainFrm.cpp : implmentation of the CMainFrame class
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "resource.h"

#include "QuicksView.h"
#include "MainFrm.h"

CMainFrame::CMainFrame() : m_init(false) {}

BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
{
	if(CFrameWindowImpl<CMainFrame>::PreTranslateMessage(pMsg))
		return TRUE;

	return m_view.PreTranslateMessage(pMsg);
}

BOOL CMainFrame::OnIdle()
{
	if (!m_init) {
		ShowWindow(SW_HIDE);
		m_init = true;
	}
	return FALSE;
}

LRESULT CMainFrame::OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	RECT clientrect;
	GetClientRect(&clientrect);

	RECT r;
	r.left = 0;
	r.right = clientrect.right-clientrect.left;
	r.top = 0;
	r.bottom = clientrect.bottom-clientrect.top;
	m_hWndClient = m_view.Create(m_hWnd, r, NULL, WS_CHILD | WS_VISIBLE);
	m_view.SetBackgroundColor(RGB(0,0,0));
	m_view.SetEventMask(ENM_PROTECTED);
	HFONT hf;
	HDC hdc;
    long lfHeight;
    
    hdc = GetDC();
    lfHeight = -MulDiv(9, GetDeviceCaps(hdc, LOGPIXELSY), 72);
    ReleaseDC(hdc);

	hf = CreateFont(lfHeight, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _TEXT("Consolas"));
	m_view.SetFont(hf);
	
	CHARFORMAT cfDefault;
	cfDefault.cbSize = sizeof(CHARFORMAT);
	cfDefault.dwMask = CFM_COLOR;
	cfDefault.dwEffects = NULL;
	cfDefault.yHeight = 0;
	cfDefault.yOffset = 0;
	cfDefault.crTextColor = RGB(255,255,255);
	cfDefault.bCharSet = NULL;
	cfDefault.bPitchAndFamily = NULL;
	m_view.SetCharFormat(cfDefault, SCF_ALL);

	// register object for message filtering and idle updates
	CMessageLoop* pLoop = _Module.GetMessageLoop();
	ATLASSERT(pLoop != NULL);
	pLoop->AddMessageFilter(this);
	pLoop->AddIdleHandler(this);

	return 0;
}

LRESULT CMainFrame::OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
	// unregister message filtering and idle updates
	CMessageLoop* pLoop = _Module.GetMessageLoop();
	ATLASSERT(pLoop != NULL);
	pLoop->RemoveMessageFilter(this);
	pLoop->RemoveIdleHandler(this);

	bHandled = FALSE;
	return 1;
}

LRESULT CMainFrame::OnProtected(int idCtrl, LPNMHDR pnmh, BOOL& bHandled) {
	return m_view.OnProtected(idCtrl, pnmh, bHandled);
}

LRESULT CMainFrame::OnNextMatch(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	return m_view.OnNextMatch(wNotifyCode, wID, hWndCtl, bHandled);
}

LRESULT CMainFrame::OnPrevMatch(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	return m_view.OnPrevMatch(wNotifyCode, wID, hWndCtl, bHandled);
}
